<style scoped>
.mask{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: #000;
    opacity: 0.3;
    z-index: 9;
}
._ModifyDocument{
    position: fixed;
    top: 15%;
    left: 50%;
    transform: translateX(-50%);
    width: 1234px;
    z-index: 99;
    background-color: white;
    overflow: auto;
}
.RS_top{
    height: 40px;
    background: #f8f8f8;
    display: flex;
    justify-content: space-between;
    padding: 10px 15px;
}
.RS_top p{
    font-weight: 700;
}
.MD_middle{
    width: 1193px;
    /* border: 1px solid black; */
    margin: 10px auto;
}
.button_box{
    background: #f8f8f8;
    width: 100%;
    height: 60px;
    padding: 15px 0 15px 15px;
    display: flex;
}
.inp_box2{
    background: #f8f8f8;
    width: 100%;
    height: 60px;
    padding: 15px 0;
    margin: 15px 0;
    display: flex;
}
.inp_box{
  display: flex;
  margin: 15px 0 10px 0;
  position: relative;
  left: -7px;
  width: 1220px;
}
.inp_box p , .inp_box2 p{
  display: inline-block;
  width: 80px;
  text-align: right;
  padding: 0 5px 0 10px;
  padding-right: 5px;
  line-height: 30px;
}

.ModifyDocument input:focus{
    outline: none;
    border: 1px solid #2a8ffc;
  }
  .inp{
    border: 1px solid #dbdbdb;
    border-radius: 3px;
    height: 30px;
    padding: 0 15px;
  }
  .inp2{
    width: 160px;
  }
  .inp3{
    width: 60px;
    margin-right: 10px;
  }
  .inp4{
      width: 400px;
  }
  .inp5{
    width: 121px;
    margin-right: 10px;
  }
  .inp6{
    width: 160px;
  }
  .RS_button{
      margin-left: 1070px;
  }
  .mg{
    margin-left: 32px;
  }
</style>

<style>
.ModifyDocument .el-input__icon{
    line-height: 30px;
  }
.ModifyDocument ._ModifyDocument .inp_box .el-input__inner {
    height: 30px;
    width: 160px;
}
.ModifyDocument ._ModifyDocument .inp_box .sty .el-input__inner {
    height: 30px;
    width: 90px;
}
.ModifyDocument ._ModifyDocument .inp_box .sty2 .el-input__inner {
    height: 30px;
    width: 160px;
}
.ModifyDocument ._ModifyDocument .inp_box .sty3 .el-input__inner {
    height: 30px;
    width: 160px;
}
.ModifyDocument ._ModifyDocument .MD_middle .inp_box2 .el-input__inner {
    height: 30px;
    width: 160px;
}
.ModifyDocument ._ModifyDocument .el-checkbox.is-bordered{
    padding: 4px 20px 9px 8px;
    height: 30px;
    width: 32px;
}

</style>

<template>
  <div class="ModifyDocument">
    <div class="mask"></div>
    <div class="_ModifyDocument">
      <div class="RS_top">
        <p>改单(客户收货结账)</p>
        <i @click="closeMD" class="el-icon-close"></i>
      </div>

      <div class="MD_middle">

        <div class="button_box">
            <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
            <el-button icon="el-icon-edit-outline" @click="modification">改单</el-button>
        </div>

        <div class="inp_box">
          <p>收款日期</p>
          <el-date-picker
            class="inp2"
            v-model="gatheringTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>

          <p>折让金额</p>
          <input class="inp inp2" type="text" v-model="discountMoney">
          <p>订金</p>
          <input class="inp inp2" type="text" v-model="earnest">
          <p>收款金额</p>
          <input class="inp inp2" type="text" v-model="gatheringMoney">
          <p>收款序号</p>
          <input class="inp inp2" type="text" v-model="gatheringSequence" disabled>
        </div>

        <div class="inp_box">

          <p>收支银行</p>
          <el-autocomplete
            clearable
            class="inline-input"
            v-model="incomeExpensesBanks"
            :fetch-suggestions="querySearchBank"
            style="width:160px;">
            <i class="el-input__icon" slot="suffix" key="venNew4"></i>
          </el-autocomplete>

          <p>缴付</p>
          <el-autocomplete
            clearable
            class="inline-input"
            v-model="pay"
            :fetch-suggestions="querySearchPay"
            style="width:160px;">
            <i class="el-input__icon" slot="suffix" key="venNew4"></i>
          </el-autocomplete>

          <p>备注</p>
          <input class="inp inp2" type="text" v-model="remark">
          <p>客户编号</p>
          <input class="inp inp2" type="text" v-model="customerNo" disabled>
          <p>客户</p>
          <input class="inp inp2" type="text" v-model="customerName" disabled>
        </div>


        <div class="inp_box2">
          <p>收款序号</p>
          <div class="sty2">
          <el-input v-model="gatheringSequence" @keyup.enter.native="search"></el-input>
          </div>
        </div>

        <el-table
          height="250"
          :data="tableData"
          highlight-current-row
          :header-cell-style="{background:'#f4f4f4',padding:0,height:'40px'}"
          :cell-style="{padding:0,height:'36px'}"
          style="width: 100%">
          <el-table-column prop="gatheringSequence" label="收款序号" width="160"></el-table-column>
          <el-table-column prop="customerNo" label="客户编号"></el-table-column>
          <el-table-column prop="discountMoney" label="折让金额"></el-table-column>
          <el-table-column prop="earnest" label="订金"></el-table-column>
          <el-table-column prop="gatheringMoney" label="收款金额"></el-table-column>
          <el-table-column prop="pay" label="缴付"></el-table-column>
          <el-table-column prop="getTime" label="收款日期"></el-table-column>
          <el-table-column prop="remark" label="备注"></el-table-column>
        </el-table>
      </div>

      <!-- <el-button @click="closeMD" class="RS_button">取消</el-button>
      <el-button type="primary" @click="modification(1)">保存</el-button> -->

    </div>
  </div>
</template>
<script>
import { getObjBySequence, update, insert } from '../../../../network/customerGathering'

import today from '../../../../components/mixins/today'

export default {
  mixins: [today],
  props: {
    payList: {
      type: Array,
      default: []
    },
    bankList: {
      type: Array,
      default: []
    },
    detailList: {
      type: Array,
      default: []
    },
    customerMap: {
      type: Object,
      default: {}
    }
  },
  data(){
    return{
      gatheringTime: '',
      discountMoney: '',
      earnest: '',
      gatheringMoney: '',
      incomeExpensesBanks: '',
      pay: '',
      remark: '',
      customerNo: '',
      customerName: '',
      gatheringSequence: '',

      gatheringSequence: '',

      tableData: []
    }
  },
  watch: {
    detailList(a,b){
      if (a.length) {
        this.gatheringTime = a[0].gatheringTime
        this.discountMoney = a[0].discountMoney
        this.earnest = a[0].earnest
        this.gatheringMoney = a[0].gatheringMoney
        this.incomeExpensesBanks = a[0].incomeExpensesBanks
        this.pay = a[0].pay
        this.remark = a[0].remark
        this.customerNo = a[0].customerNo
        this.customerName = this.customerMap[a[0].customerNo]
        this.gatheringSequence = a[0].gatheringSequence
        this.tableData = a
      } else {
        this.gatheringTime = ''
        this.discountMoney = ''
        this.earnest = ''
        this.gatheringMoney = ''
        this.incomeExpensesBanks = ''
        this.pay = ''
        this.remark = ''
        this.customerNo = ''
        this.customerName = ''
        this.gatheringSequence = ''
        this.tableData = []
      }
    }
  },
  methods:{
    clearData(){
      this.gatheringTime = ''
      this.discountMoney = ''
      this.earnest = ''
      this.gatheringMoney = ''
      this.incomeExpensesBanks = ''
      this.pay = ''
      this.remark = ''
      this.customerNo = ''
      this.customerName = ''
      this.gatheringSequence = ''
      this.tableData = []
    },
    closeMD(){
      this.$emit('closeMD')
      this.clearData()
    },

    querySearchPay(queryString, cb) {
      cb(this.payList)
    },
    querySearchBank(queryString, cb) {
      cb(this.bankList)
    },
    
    // 查询
    search(){
      if (!this.gatheringSequence) {
        this.$alert('请输入序号查询', '', {
          confirmButtonText: '确定'
        })
        return
      }
      getObjBySequence({sequence:this.gatheringSequence}).then(res => {
        // console.log(res)
        if (res.data.code == 200) {
          res.data.data.getTime = res.data.data.gatheringTime.slice(0,10)
          this.tableData = [res.data.data]
          this.gatheringTime = res.data.data.gatheringTime
          this.discountMoney = res.data.data.discountMoney
          this.earnest = res.data.data.earnest
          this.gatheringMoney = res.data.data.gatheringMoney
          this.incomeExpensesBanks = res.data.data.incomeExpensesBanks
          this.pay = res.data.data.pay
          this.remark = res.data.data.remark
          this.customerNo = res.data.data.customerNo
          this.customerName = this.customerMap[res.data.data.customerNo]
          this.gatheringSequence = res.data.data.gatheringSequence
          this.tableData = [res.data.data]
        }
      })
    },

    
    modification(f){
      if (!String(this.discountMoney)) {
        this.$alert('折让金额不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!String(this.gatheringMoney)) {
        this.$alert('收款金额不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!String(this.gatheringTime)) {
        this.$alert('收款日期不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!String(this.earnest)) {
        this.$alert('订金不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      let d = this.tableData[0]
      d.gatheringSequence = this.gatheringSequence
      d.customerNo = String(this.customerNo)
      d.discountMoney = this.discountMoney
      d.gatheringMoney = this.gatheringMoney
      d.gatheringTime = this.gatheringTime
      d.earnest = this.earnest
      d.pay = this.pay
      d.incomeExpensesBanks = this.incomeExpensesBanks
      d.receivedOtherMoney = this.receivedOtherMoney
      d.remark = this.remark
      d.curency = '￥'
      // console.log(d)
      update(JSON.stringify(d)).then(res => {
        // console.log(res)
        if (res.data.code == 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.search()
          if (this.pay || this.incomeExpensesBanks) {
            insert(JSON.stringify({
              pay: this.pay,
              bank: this.incomeExpensesBanks
            })).then(res => {
              // console.log(res)
            })
          }
        } else {
          this.$message.error('修改失败')
        }
      })
    },
  }
}
</script>